# 对Compare信息进行处理
module Aio::Text
  class Compare

    attr_accessor :cmd

    # 比较和被比较的差异值
    def initialize
      @cmd = {}
    end

    # 以每个cmd分类比较信息
    def <<(info)
      cmd_name = info[:e].shift
      
      cmd[cmd_name] ||= CompareDiff.new
      cmd[cmd_name] << info
    end

    def clear
      @cmd = {}
    end

    def size
      res = 0
      cmd.values do |cd|
        res += cd.size
      end
      res
    end

    def empty?
      cmd.empty?
    end

    def each_cmd_with_diff
      @cmd.each_pair do |c, e|
        yield c, e
      end
    end
  end

  class CompareDiff

    # cm1 = [ MatchStringInfo_A, MatchStringInfo_A, .. ]
    attr_accessor :cm1, :cm2

    def initialize
      @cm1 = []
      @cm2 = []
    end

    # info #=> { :cm => :cm1, :e => [cmd, key, match_string_info] }
    def <<(info)
      case info[:cm]
      when :cm1
        @cm1 << info[:e].pop
      when :cm2
        @cm2 << info[:e].pop
      end
    end

    # 两个文本集合
    def content_muster
      { cm1: content, cm2: compare_content }
    end

    # 文本
    def content
      return [""] if cm1.first.nil?
      cm1.first.content
    end

    # 比较的文本 
    def content_compare
      return [""] if cm2.first.nil?
      cm2.first.content
    end

    def size
      cm1.size
    end

    def lines
      res = []
      cm1.each do |e|
        res << e.line
      end

      res.uniq
    end

    def lines_compare
      res = []
      cm2.each do |e|
        res << e.line
      end

      res.uniq
    end

    # 所有不同行的集合
    def lines_muster
      { cm1: lines, cm2: lines_compare }
    end

  end
end

